package com.cloudcc.mobile.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudcc.mobile.R;

/**
 * Created by lin on 2017/6/12.
 */

public class CallLogLoadingDialog extends Dialog {

    private ImageView imageView;
    private TextView text;
    private Animation operatingAnim;//旋转动画


    public CallLogLoadingDialog(@NonNull Context context) {
        super(context);
    }

    public CallLogLoadingDialog(@NonNull Context context, @StyleRes int theme) {
        super(context, theme);
    }

    protected CallLogLoadingDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_call_log_loading);
        getWindow().setBackgroundDrawableResource(R.color.transparent);
        imageView = (ImageView) findViewById(R.id.call_log_dialog_loading_iv);
        text = (TextView) findViewById(R.id.call_log_dialog_loading_tv);
        operatingAnim = AnimationUtils.loadAnimation(getContext(), R.anim.refresh_rotate_anim);
        LinearInterpolator lin = new LinearInterpolator();
        operatingAnim.setInterpolator(lin);
        imageView.startAnimation(operatingAnim);
    }

    public void settext(String texts) {
        text.setText(texts);
    }

    @Override
    public void show() {
        if(operatingAnim != null  && imageView != null && !operatingAnim.hasStarted()){
            imageView.startAnimation(operatingAnim);
        }
        super.show();
    }

    @Override
    public void dismiss() {
        if(operatingAnim != null && operatingAnim.hasStarted() && imageView != null){
            imageView.clearAnimation();
        }
        super.dismiss();
    }
}
